<?php

App::import('Controller', 'Users');
App::import('Model', 'Account');

class PropertiesController extends AppController {
    var $name = 'Properties';

    function index() {
        $this->Property->recursive = 0;
	//	$permit  = $this->Session->read('permit_property');
           // if ($permit != null){
	     //       $data = null;      
    	     //   $new  = null;                                                        
              //  foreach ($permit as $key => $value) {
              //      $this->paginate = array(
              //                   'conditions' => array( 'Property.property_id' => $value["property_id"] )
              //               );
              //      $data[$key]  = $this->paginate();
             //   }
            //    foreach ( $data as $key1 => $value1)
            //    {
            //        $new[$key1] = $value1[0];
            //    }  
           //         $this->set('properties', $new);
         //   }else{
       //       $this->Session->setFlash(__('You have nothing assigned', true));
     //         $this->redirect(array('controller' => 'users', 'action' => 'welcome'));
     //       }   

   $this->set('properties', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid property', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('property', $this->Property->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->Property->create();
            $this->data['PropertyType']['end_date'] = null;
			if ($this->Property->save($this->data)) {
                $this->Session->setFlash(__('The property has been saved', true));
                $sql = "SELECT max(property_id) id
                        FROM   properties";
                $new_id = $this->Property->query($sql);
                // Insersion default
                $account = new Account();
                $account_data = array();
                $account_data["account_id"] = null;
                $account_data["property_id"] = $new_id['0']['0']['id'];
                $account_data["special_expense_balance"] = 0;
                $account_data["special_interest_balance"] = 0;
                $account_data["regular_expense_balance"] = 0;
                $account_data["regular_interests_balance"] = 0;
                $account->save($account_data);

				$sql = " SELECT building_id,property_id
						 FROM   properties 
						 order by property_id desc";
				$ids = $this->Property->query($sql);
				$property_id  = $ids[0]["properties"]["property_id"];
				$building_id  = $ids[0]["properties"]["building_id"];
				$user = new UsersController;
				$user->constructClasses();
				$user->add_property($building_id,$property_id);
			    $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The property could not be saved. Please, try again.', true));
            }
        }
        $buildings = $this->Property->Building->find('list');
//      $propertyTypes = $this->Property->PropertyType->find('list');
        $owners = $this->Property->Owner->find('list');
        $ownerRepresentatives = $this->Property->OwnerRepresentative->find('list');
        $tenants = $this->Property->Tenant->find('list');
        $tenantRepresentatives = $this->Property->TenantRepresentative->find('list');
        $this->set(compact('buildings', 'propertyTypes', 'owners', 'ownerRepresentatives', 'tenants', 'tenantRepresentatives'));
	}

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid property', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Property->save($this->data)) {
                $this->Session->setFlash(__('The property has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The property could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Property->read(null, $id);
        }
        $buildings = $this->Property->Building->find('list');
        $propertyTypes = $this->Property->PropertyType->find('list');
        $owners = $this->Property->Owner->find('list');
        $ownerRepresentatives = $this->Property->OwnerRepresentative->find('list');
        $tenants = $this->Property->Tenant->find('list');
        $tenantRepresentatives = $this->Property->TenantRepresentative->find('list');
        $this->set(compact('buildings', 'propertyTypes', 'owners', 'ownerRepresentatives', 'tenants', 'tenantRepresentatives'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for property', true));
            $this->redirect(array('action'=>'index'));
        }
        if (empty($this->data)) {
            $this->data = $this->Property->read(null, $id);
        }
        if (!empty($this->data)) {
            // ACA SETEO EL UPDATED_BY
            $this->data['Property']['end_date'] = date('Y-m-d H:i:s');
            if ($this->Property->save($this->data)) {
                $this->Session->setFlash(__('Property deleted', true));
            } else {
                $this->Session->setFlash(__('Property was not deleted', true));
            }
        }
        $this->redirect(array('action' => 'index'));
    }
    
    function update_select(){

	    if(!empty($this->data['Property']['building_id'])) {              
	        $building = $this->data['Property']['building_id'];
	
	        $options = $this->Property->PropertyType ->find('list',
	        array('conditions' => array('PropertyType.building_id' => $building)));
	        
	      }
		
    	$this->set(compact('options'));      
	}
    
}
?>
